/**
 * Created by sunga on 2019/1/2.
 */

$().ready(function () {

    $("#personalize_edit").change(function () {
        if ($("#personalize_edit").is(':checked') == true) {
            $("#personalize_eed").css("display", "block");
            console.log(1111123333);
        } else {
            $("#personalize_eed").css("display", "none");
        }
    });

    $(".editproj").click(function () {
        $('#ingress_info').modal({backdrop: 'static', keyboard: false});
        console.log(1111);
        var proj_id = $(this).attr("data-proj-id");
        console.log(proj_id);
        $.ajax({
            url: "/serviceinfo/http_detail/",
            type: "post",
            data: {"pro_id": proj_id},
            success: function (data) {
                console.log(data);
                data = JSON.parse(data);
                if (data["status"] == 200) {
                    data = data["app_info"];
                    console.log(data);
                    $("#proj_iddd").val('');
                    $("#proj_iddd").val(data["proj_id"]);

                    $("#domain_addr").val('');
                    $("#domain_addr").val(data["domain"]);

                    // 确定环境类型
                    $("#app_port").val('');
                    $("#app_port").val(data["port"]);

                    $("#request_path").val('');
                    $("#request_path").val(data["request_path"]);

                    $("#service_path").val('');
                    console.log(111343431);
                    $("#service_path").val(data["service_path"]);

                    $("#personalize").val('');
                    $("#personalize_edit").prop("checked","");
                    $("#personalize_eed").css("display", "none");
                    if(data["personalize"] == 1){
                        console.log(4444444);
                        $("#personalize_edit").prop("checked","checked");
                        $("#personalize_eed").css("display", "block");
                        $("#personalize").val(data["persontext"]);
                    }

                }
            }
        })
    });

    $("#save_http_app").click(function () {
        // console.log(111333331);
        var linkss = $(".active a").attr("href");
        var proj_id = $("#proj_iddd").val();
        console.log(proj_id);
        var hostname = $("#domain_addr").val();
        var port = $("#app_port").val();
        var request_path = $("#request_path").val();
        var service_path = $("#service_path").val();
        if($("#personalize_edit").is(':checked') == true){
            var personalize = 1;
        }else {
            var personalize = 2;
        }
        var personalizetext = $("#personalize").val();
        console.log(personalize);
        console.log(personalizetext);

        $.ajax({
            url: "/serviceinfo/httpinfo/edit/" + proj_id + "/",
            type: "post",
            data: {
                "proj_id": proj_id,
                "hostname": hostname,
                "port": port,
                "request_path": request_path,
                "service_path": service_path,
                "personalize":personalize,
                "personalizetext":personalizetext

            },
            success: function (data) {
                var data = JSON.parse(data);
                console.log(data);
                if (data["status"] == 200) {
                    // window.location.href = '/serviceinfo/';
                    swal({
                        title: "",
                        text: "应用更新成功"
                    }, function () {
                        // window.location.href = '/service/connservice_list/?env_type='+ +'&search_app='+ ;
                        window.location.href = linkss;
                    })
                } else if (data["status"] == 500) {
                    swal({
                        title: "",
                        text: "应用更新失败"
                    });
                    console.log("eeeeeeee");
                }else if (data["status"] == 400) {
                    swal({
                        title: "",
                        text: "<span style='color:#FF0000'>请求路径已被" + data["appname"] + "应用使用，请检查确认</span>",
                        html:true,

                    });
                    console.log("eeeeeeee");
                }
            }
        })
    });

    $(".selectproj").click(function () {
        $('#select_info').modal({backdrop: 'static', keyboard: false});
        var proj_id = $(this).attr("data-proj-id");
        $.ajax({
            url: "/serviceinfo/http_detail/",
            type: "post",
            data: {"pro_id": proj_id},
            success: function (data) {
                console.log(11111112222);
                console.log(data);
                data = JSON.parse(data);
                console.log(data);
                data = data["app_info"];
                $("#proj_iddds").val('');
                $("#proj_iddds").val(data["proj_id"]);

                $("#domain_addrs").val('');
                $("#domain_addrs").val(data["domain"]);

                // 确定环境类型
                $("#app_ports").val('');
                $("#app_ports").val(data["port"]);

                $("#request_paths").val('');
                $("#request_paths").val(data["request_path"]);

                $("#service_paths").val('');
                $("#service_paths").val(data["service_path"]);

                $("#personalizess").val('');
                if(data["personalize"] == 1){
                        console.log(55555);
                        $("#personalize_select").prop("checked","checked");
                        $("#personalize_ses").css("display", "block");
                        $("#personalizess").val(data["persontext"]);
                    }
            }
        })
    });


    $("#ingress_build").click(function () {
        var all_apps = new Array();
        $('[data-app-idss="application-app"]').each(function () {
            if ($(this).is(':checked') == true) {
                all_apps.push($(this).attr("data-appss-id"));
            }
        });
    })

    $(".updateproj").click(function () {
        var proj_id = $(this).attr("data-proj-id");
        var linkss = $(".active a").attr("href");
        console.log(proj_id);
        console.log(linkss);
        $.ajax({
            url: "/serviceinfo/httpinfo/no_domain/",
            type: "post",
            data: {"proj_id": proj_id},
            success: function (data) {
                console.log(data);
                data = JSON.parse(data);
                console.log(data);
                if (data["status"] == 200) {
                    // window.location.href = '/serviceinfo/';
                    swal({
                        title: "",
                        text: "应用重置完成"
                    }, function () {
                        // window.location.href = '/service/connservice_list/?env_type='+ +'&search_app='+ ;
                        window.location.href = linkss;
                    })
                } else if (data["status"] == 500) {
                    swal({
                        title: "",
                        text: "应用重置失败"
                    }, function () {
                        window.location.href = linkss;
                    });
                }

            }
        })
    })
    $(".ingress_create").click(function () {
        var proj_id = $(this).attr("data-proj-id");
        var linkss = $(".active a").attr("href");
        var path_in = $(this).parent().prev().html();
        if (path_in == '') {
            console.log(3434343434);
            swal({
                title: "",
                text: "必须先编辑才能点击创建!"
            })
        } else {
            $.ajax({
                url: "/serviceinfo/httpinfo/ingress/",
                type: "post",
                data: {"proj_id": proj_id},
                success: function (data) {
                    console.log(data);
                    data = JSON.parse(data);
                    if (data["status"] == 200) {
                        // window.location.href = '/serviceinfo/';
                        swal({
                            title: "",
                            text: "NGINX创建成功"
                        }, function () {
                            // window.location.href = '/service/connservice_list/?env_type='+ +'&search_app='+ ;
                            window.location.href = linkss;
                        })
                    } else if (data["status"] == 500) {
                        swal({
                            title: "",
                            text: "NGINX创建失败"
                        });
                        console.log("eeeeeeee");
                    }
                }
            })
        }
    })
});